//
//  PopulationBaseInfoViewController.m
//  Utils
//
//  Created by enghou on 2018/4/20.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//
#import "PopulationContainerViewController.h"
#import "PopulationBaseInfoViewController.h"
@interface PopulationBaseInfoViewController ()<UIPickerViewDelegate,UIPickerViewDataSource,UITextFieldDelegate>

@property(nonatomic,strong)UITextField *dateField;

@property(nonatomic,strong)UIDatePicker *datePicker;


- (IBAction)showBirthDatePicker:(id)sender;



@property(nonatomic,copy)NSArray *registerTypeArray;//户口类型

@property(nonatomic,copy)NSArray *sexArray;


@property(nonatomic,copy)NSArray *minzuArray;//民族数组

@property(nonatomic,copy)NSArray *jiguanArray;//籍贯
@property(nonatomic,strong)PropertyDictItemModel *jiguanModel;//籍贯
@property(nonatomic,strong)PropertyDictItemModel *nowLocateModel;//现住地
@property(nonatomic,copy)NSArray *politicals;//政治面貌
@property(nonatomic,copy)NSArray *marryArray;
@property(nonatomic,copy)NSArray *occupyArray;
@property(nonatomic,copy)NSArray *religionArray;

//户籍

@property(nonatomic,strong)NSArray *provinces;
@property(nonatomic,strong)UIPickerView *provincePicker;
//籍贯
@property(nonatomic,strong)UITextField *provinceField;
//现住地
@property(nonatomic,strong)UITextField *nowField;



@end

@implementation PopulationBaseInfoViewController

-(instancetype)init{
    self = [super init];
    if (self) {
        self.paramPrefix = @"population";
    }
    return self;
}

-(UIImage *)headImage{
    return self.headImageView.image;
}

-(NSDictionary *)param{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    if ([self.ids length] > 0) {
        [param setObject:self.ids forKey:[NSString stringWithFormat:@"%@.id",self.paramPrefix]];
    }
    if ([self.idCardField.text length] > 0) {
        [param setObject:self.idCardField.text forKey:[NSString stringWithFormat:@"%@.idCardNo",self.paramPrefix]];
    }else{
        [self alertSomes:@"请输入身份证号码!"];
        return nil;
    }
    if ([self.nameField.text length] > 0) {
        [param setObject:self.nameField.text forKey:[NSString stringWithFormat:@"%@.name",self.paramPrefix]];
    }else{
        [self alertSomes:@"请输入人口姓名!"];
        return nil;
    }
    if ([self.sexModel.ids length] > 0) {
        [param setObject:self.sexModel.ids forKey:[NSString stringWithFormat:@"%@.gender.id",self.paramPrefix]];
    }else{
        [self alertSomes:@"请选择性别"];
        return nil;
    }
    if ([self.birthdayField.text length] > 0) {
        [param setObject:self.birthdayField.text forKey:[NSString stringWithFormat:@"%@.birthday",self.paramPrefix]];
    }else{
        [self alertSomes:@"请选择出生日期"];
        return nil;
    }
    if ([self.registerTypeModel.ids length] > 0) {
        [param setObject:self.registerTypeModel.ids forKey:[NSString stringWithFormat:@"%@.residenceType.id",self.paramPrefix]];
    }else{
        [self alertSomes:@"请选择户口类型"];
        return nil;
    }
    if ([self.minzuModel.ids length] > 0) {
//        [NSString stringWithFormat:@"%@.nation.id",self.paramPrefix]
        [param setObject:self.minzuModel.ids forKey:[NSString stringWithFormat:@"%@.nation.id",self.paramPrefix]];
    }else{
        [self alertSomes:@"请选择民族类型"];
        return nil;
    }
    if ([self.hujiProvince.code length] > 0 && [self.hujiCity.code length] > 0 && [self.hujiCountry.code length] > 0) {
        
        [param setObject:self.hujiProvince.code forKey:[NSString stringWithFormat:@"%@.province",self.paramPrefix]];
        [param setObject:self.hujiCity.code forKey:[NSString stringWithFormat:@"%@.city",self.paramPrefix]];
        [param setObject:self.hujiCountry.code forKey:[NSString stringWithFormat:@"%@.district",self.paramPrefix]];
    }else{
        [self alertSomes:@"请选择户籍所在地"];
        return nil;
    }
    if ([self.oldProvince.code length] > 0 && [self.oldCity.code length] > 0 && [self.oldCountry.code length] > 0) {
//        [NSString stringWithFormat:@"%@.placeProvince",self.paramPrefix]
        [param setObject:self.oldProvince.code forKey:[NSString stringWithFormat:@"%@.placeProvince",self.paramPrefix]];
        [param setObject:self.oldCity.code forKey:        [NSString stringWithFormat:@"%@.placeCity",self.paramPrefix]
];
        [param setObject:self.oldCountry.code forKey:        [NSString stringWithFormat:@"%@.placeDistrict",self.paramPrefix]
];
    }else{
        [self alertSomes:@"请选择籍贯!"];
        return nil;
    }
    if ([self.nowProvince.code length] > 0 && [self.nowCity.code length] > 0 && [self.nowCountry.code length] > 0) {

        [param setObject:self.nowProvince.code forKey:[NSString stringWithFormat:@"%@.presentProvince",self.paramPrefix]];
        [param setObject:self.nowCity.code forKey:[NSString stringWithFormat:@"%@.presentCity",self.paramPrefix]];
        [param setObject:self.nowCountry.code forKey:[NSString stringWithFormat:@"%@.presentDistrict",self.paramPrefix]];
    }else{
        [self alertSomes:@"请选择现住地!"];
        return nil;
    }
//    population.currentAddress
    if (self.yesBtn.isSelected) {
        [param setObject:@"true" forKey:[NSString stringWithFormat:@"%@.isHaveHouse",self.paramPrefix]];
    }else{
        [param setObject:@"false" forKey:[NSString stringWithFormat:@"%@.isHaveHouse",self.paramPrefix]];
    }
    if ([self.homeLocateDetailField.text length] > 0) {
        [param setObject:self.homeLocateDetailField.text forKey:[NSString stringWithFormat:@"%@.nativePlaceAddress",self.paramPrefix]];
    }else{
        [self alertSomes:@"请输入户籍门楼详址!"];
        return nil;
    }
    if ([self.nowLocateDetailField.text length] > 0) {
        [param setObject:self.nowLocateDetailField.text forKey:[NSString stringWithFormat:@"%@.currentAddress",self.paramPrefix]];
    }else{
        [self alertSomes:@"请输入现住门楼详址或者无住房原因!"];
        return nil;
    }
    if ([self.politicalModel.ids length] > 0) {
        [param setObject:self.politicalModel.ids forKey:[NSString stringWithFormat:@"%@.politicalBackground.id",self.paramPrefix]];
    }else{
        [self alertSomes:@"请选择政治面貌!"];
        return nil;
    }
    if ([self.marryModel.ids length] > 0) {
//        [NSString stringWithFormat:@"%@.maritalState.id",self.paramPrefix]
        [param setObject:self.marryModel.ids forKey:[NSString stringWithFormat:@"%@.maritalState.id",self.paramPrefix]];
    }else{
        [self alertSomes:@"请选择婚姻状态!"];
        return nil;
    }
    if ([self.pastNameField.text length] > 0) {
        [param setObject:self.pastNameField.text forKey:[NSString stringWithFormat:@"%@.usedName",self.paramPrefix]];
    }
    if (!(self.phoneNumberField.text.length > 0) && !(self.stablePhoneField.text.length > 0)) {
        [self alertSomes:@"请输入联系方式"];
        return nil;
    }else{
        if ([self.phoneNumberField.text length] > 0) {
//            [NSString stringWithFormat:@"%@.mobileNumber",self.paramPrefix]
            [param setObject:self.phoneNumberField.text forKey:[NSString stringWithFormat:@"%@.mobileNumber",self.paramPrefix]];
        }
        if ([self.stablePhoneField.text length] > 0) {
            [param setObject:self.stablePhoneField.text forKey:[NSString stringWithFormat:@"%@.telephone",self.paramPrefix]];
        }
    }
    if ([self.eduDegreeModel.subTid length] > 0) {
        [param setObject:self.eduDegreeModel.subTid forKey:[NSString stringWithFormat:@"%@.schoolStr",self.paramPrefix]];
    }else{
        [self alertSomes:@"请选择学历"];
        return nil;
    }
    if ([self.occupyModel.ids length] > 0) {
        [param setObject:self.occupyModel.ids forKey:[NSString stringWithFormat:@"%@.career.id",self.paramPrefix]];
    }else{
        [self alertSomes:@"请选择职业类别"];
        return nil;
    }
    if (self.occupyField.text.length > 0) {
        [param setObject:self.occupyField.text forKey:[NSString stringWithFormat:@"%@.careerText",self.paramPrefix]];
    }else{
        [self alertSomes:@"请输入职业名称!"];
        return nil;
    }
    if ([self.religionModel.ids length] > 0) {
        [param setObject:self.religionModel.ids forKey:[NSString stringWithFormat:@"%@.faith.id",self.paramPrefix]];
    }
    if (self.serviceLocateField.text.length > 0) {
        [param setObject:self.serviceLocateField.text forKey:[NSString stringWithFormat:@"%@.workUnit",self.paramPrefix]];
    }
    return param;
}

-(void)alertSomes:(NSString *)msg{
    PopulationContainerViewController *container = (PopulationContainerViewController *)self.theDelete;
    [container turnToIndex:[container.ctlArray indexOfObject:self] andAlert:msg];
}

- (IBAction)yesAction:(id)sender {
    if (self.canEdit) {
        self.yesBtn.selected = YES;
        self.noBtn.selected = NO;
        NSMutableAttributedString *target = [[NSMutableAttributedString alloc]initWithString:@"*现住门(楼)详址" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"333333"],NSFontAttributeName:[UIFont systemFontOfSize:16]}];
        self.nowLocateDetailField.placeholder = @"请输入门楼详址";
        [target addAttributes:@{NSForegroundColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:17.f]} range:NSMakeRange(0, 1)];
        self.changeState.attributedText = target;
    }
}
- (IBAction)noAction:(id)sender {
    if (self.canEdit) {
        self.yesBtn.selected = NO;
        self.noBtn.selected = YES;
        NSMutableAttributedString *target = [[NSMutableAttributedString alloc]initWithString:@"*无住房原因" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"333333"],NSFontAttributeName:[UIFont systemFontOfSize:16]}];
        [target addAttributes:@{NSForegroundColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:16]} range:NSMakeRange(0, 1)];
        self.changeState.attributedText = target;
        self.nowLocateDetailField.placeholder = @"请输入无住房原因";
    }
}

- (IBAction)showBirthDatePicker:(id)sender {
    [self.dateField becomeFirstResponder];
}

-(void)sureAction{
    if ([self.dateField isFirstResponder]) {
        self.birthdayField.text = [[self.datePicker date]dateLineStrOnlyDay];
    }else if ([self.nowField isFirstResponder]){
        //现住地
        NSInteger provinceIdx = [_provincePicker selectedRowInComponent:0];
        NSInteger cityIdx = [_provincePicker selectedRowInComponent:1];
        NSInteger countryIdx = [_provincePicker selectedRowInComponent:2];
        _nowProvince = _provinces[provinceIdx];
        _nowCity = _nowProvince.citys[cityIdx];
        _nowCountry = _nowCity.countrys[countryIdx];
        self.nowLocateField.text = [NSString stringWithFormat:@"%@%@%@",_nowProvince.name,_nowCity.name,_nowCountry.name];
    }else if ([self.provinceField isFirstResponder]){
        NSInteger provinceIdx = [_provincePicker selectedRowInComponent:0];
        NSInteger cityIdx = [_provincePicker selectedRowInComponent:1];
        NSInteger countryIdx = [_provincePicker selectedRowInComponent:2];
        _oldProvince = _provinces[provinceIdx];
        _oldCity = _oldProvince.citys[cityIdx];
        _oldCountry = _oldCity.countrys[countryIdx];
        self.homeLocateField.text = [NSString stringWithFormat:@"%@%@%@",_oldProvince.name,_oldCity.name,_oldCountry.name];
    }
    [self.view endEditing:YES];
}

-(void)cancelAction{
    [self.view endEditing:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.paramPrefix = @"population";
    self.dateField = [[UITextField alloc]init];
    [self.view addSubview:self.dateField];
    self.datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 240)];
    self.datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
    self.datePicker.backgroundColor = [UIColor whiteColor];
    self.datePicker.datePickerMode = UIDatePickerModeDate;
    self.datePicker.minimumDate = [NSDate date];
    self.dateField.inputView = self.datePicker;
    self.dateField.inputAccessoryView = [self inputAccessView];
    
    
    self.provincePicker = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 240)];
    self.provincePicker.backgroundColor = [UIColor whiteColor];
    self.provincePicker.delegate = self;
    self.provincePicker.dataSource = self;
    self.provinceField = [[UITextField alloc]init];
    [self.view addSubview:self.provinceField];
    self.provinceField.inputView = self.provincePicker;
    self.provinceField.inputAccessoryView = [self inputAccessView];
    
    self.nowField = [[UITextField alloc]init];
    [self.view addSubview:self.nowField];
    self.nowField.inputView = self.provincePicker;
    self.nowField.inputAccessoryView = [self inputAccessView];
}

-(void)setParam:(NSDictionary *)param{
//    _param = param;
    //
}


-(void)getInfoWithId:(NSString *)uid{
    
}


-(void)showImagePicker{
    TZImagePickerController *picker = [[TZImagePickerController alloc]initWithMaxImagesCount:1 delegate:nil];
    picker.didFinishPickingPhotosHandle = ^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        UIImage *image = photos[0];
        self.headImageView.image = image;
        self.headchanged = YES;
    };
    [(BaseViewController *)self.theDelete presentViewController:picker animated:YES completion:nil];
}

-(void)commonAlertWithContent:(NSString *)content choosenBlock:(void(^)(PropertyDictItemModel *))choosen getList:(void(^)(NSArray *))getList{
    [self beginLoading];
    [[NetWorkerManager sharedInstance]getPropertyDictWithDomainName:content successBlock:^(BaseModel *model) {
        [self endLoading];
        if (model.success) {
            getList(model.list);
            [self alertWithArray:model.list choosenBock:choosen];
        }else{
            [self alertSome:model.msg];
        }
    } failBlock:^(NSInteger code) {
        [self endLoading];
        [self alertSome:kNetWorkFail];
    }];
}

-(void)alertWithArray:(NSArray <PropertyDictItemModel *>*)array choosenBock:(void(^)(PropertyDictItemModel *))choosen{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    for (PropertyDictItemModel *model in array) {
        UIAlertAction *action = [UIAlertAction actionWithTitle:model.displayName style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            choosen(model);
        }];
        [alert addAction:action];
    }
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];
}

#pragma mark 选择性别
-(void)chooseSex{
    [self commonAlertWithContent:@"性别" choosenBlock:^(PropertyDictItemModel *model) {
        self.sexModel = model;
        self.sexField.text = model.displayName;
    } getList:^(NSArray *array) {
        self.sexArray = array;
    }];
//    if (self.sexArray) {
//        [self alertWithArray:self.sexArray choosenBock:^(PropertyDictItemModel *model) {
//            self.sexModel = model;
//            self.sexField.text = model.displayName;
//        }];
//    }else{
//        [self commonAlertWithContent:@"性别" choosenBlock:^(PropertyDictItemModel *model) {
//            self.sexModel = model;
//            self.sexField.text = model.displayName;
//        } getList:^(NSArray *array) {
//            self.sexArray = array;
//        }];
//    }
}

#pragma mark 选择户口类型
-(void)chooseRegisterType{
    [self commonAlertWithContent:@"户口类别" choosenBlock:^(PropertyDictItemModel *model) {
        self.registerTypeModel = model;
        self.locateTypeField.text = model.displayName;
    } getList:^(NSArray *array) {
        self.registerTypeArray = array;
    }];
}

#pragma mark 选择民族
-(void)chooseNation{
    [self commonAlertWithContent:@"民族" choosenBlock:^(PropertyDictItemModel *model) {
        self.minzuModel = model;
        self.minzuField.text = self.minzuModel.displayName;
    } getList:^(NSArray *array) {
        self.minzuArray = array;
    }];
//    if (self.minzuArray) {
//        [self alertWithArray:self.minzuArray choosenBock:^(PropertyDictItemModel *model) {
//            self.minzuModel = model;
//            self.minzuField.text = self.minzuModel.displayName;
//        }];
//    }else{
//        [self commonAlertWithContent:@"民族" choosenBlock:^(PropertyDictItemModel *model) {
//            self.minzuModel = model;
//            self.minzuField.text = self.minzuModel.displayName;
//        } getList:^(NSArray *array) {
//            self.minzuArray = array;
//        }];
//    }
}

#pragma mark 选择现住地
-(void)chooseNowLocate{
    if (!self.provinces) {
        self.provinces = [ProvincesGenerator generatProvinces];
    }
    [self.nowField becomeFirstResponder];
}

#pragma mark - 选择籍贯
-(void)chooseBornLocate{
    if (!self.provinces) {
        self.provinces = [ProvincesGenerator generatProvinces];
    }
    [self.provinceField becomeFirstResponder];
}

#pragma mark - 选择政治面貌
-(void)choosePolitical{
    [self commonAlertWithContent:@"政治面貌" choosenBlock:^(PropertyDictItemModel *model) {
        self.politicalModel = model;
        self.zhengzhiField.text = self.politicalModel.displayName;
    } getList:^(NSArray *array) {
        self.politicals = array;
    }];
//    if (self.politicals) {
//        [self alertWithArray:self.politicals choosenBock:^(PropertyDictItemModel *model) {
//            self.politicalModel = model;
//            self.zhengzhiField.text = self.politicalModel.displayName;
//        }];
//    }else{
//        [self commonAlertWithContent:@"政治面貌" choosenBlock:^(PropertyDictItemModel *model) {
//            self.politicalModel = model;
//            self.zhengzhiField.text = self.politicalModel.displayName;
//        } getList:^(NSArray *array) {
//            self.politicals = array;
//        }];
//    }
}

#pragma mark - 选择婚姻状况
-(void)chooseMarried{
    if (self.marryArray) {
        [self alertWithArray:self.marryArray choosenBock:^(PropertyDictItemModel *model) {
            self.marryModel = model;
            self.marriedField.text = model.displayName;
        }];
    }else{
        [self commonAlertWithContent:@"婚姻状况" choosenBlock:^(PropertyDictItemModel *model) {
            self.marryModel = model;
            self.marriedField.text = model.displayName;
        } getList:^(NSArray *array) {
            self.marryArray = array;
        }];
    }
}

#pragma mark - 选择学历
-(void)chooseEduDegree{
    QueryTypeSelectionController *query = (QueryTypeSelectionController *)[VCLoader vcWithName:@"QueryTypeSelectionController"];
    query.path = kEducationDegree;
    query.itemCls = @"DegreeListModel";
    query.theDelete = self;
    [(BaseViewController *)self.theDelete push:query];
}

#pragma mark - 选择职业类别
-(void)chooseOccupyCategory{
    [self commonAlertWithContent:@"职业" choosenBlock:^(PropertyDictItemModel *model) {
        self.occupyModel = model;
        self.occupyCategoryField.text = model.displayName;
    } getList:^(NSArray *array) {
        self.occupyArray = array;
    }];
//    if (self.occupyArray) {
//        [self alertWithArray:self.occupyArray choosenBock:^(PropertyDictItemModel *model) {
//            self.occupyModel = model;
//            self.occupyCategoryField.text = model.displayName;
//        }];
//    }else{
//        [self commonAlertWithContent:@"职业" choosenBlock:^(PropertyDictItemModel *model) {
//            self.occupyModel = model;
//            self.occupyCategoryField.text = model.displayName;
//        } getList:^(NSArray *array) {
//            self.occupyArray = array;
//        }];
//    }
}

#pragma mark - 选择宗教信仰
-(void)chooseReligon{
    [self commonAlertWithContent:@"宗教信仰" choosenBlock:^(PropertyDictItemModel *model) {
        self.religionModel = model;
        self.zongjiaoField.text = model.displayName;
    } getList:^(NSArray *array) {
        self.religionArray = array;
    }];
//    if (self.religionArray) {
//        [self alertWithArray:self.religionArray choosenBock:^(PropertyDictItemModel *model) {
//            self.religionModel = model;
//            self.zongjiaoField.text = model.displayName;
//        }];
//    }else{
//        [self commonAlertWithContent:@"宗教信仰" choosenBlock:^(PropertyDictItemModel *model) {
//            self.religionModel = model;
//            self.zongjiaoField.text = model.displayName;
//        } getList:^(NSArray *array) {
//            self.religionArray = array;
//        }];
//    }
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.canEdit) {
        if (indexPath.row == 0) {
            [self showImagePicker];
        }else if (indexPath.row == 3){
            [self chooseSex];
        }else if (indexPath.row == 5){
            [self chooseRegisterType];
        }else if (indexPath.row == 6){
            [self chooseNation];
        }else if (indexPath.row == 8){
            [self chooseBornLocate];
        }else if (indexPath.row == 9){
            [self chooseNowLocate];
        }else if (indexPath.row == 13){
            [self choosePolitical];
        }else if (indexPath.row == 14){
            [self chooseMarried];
        }else if (indexPath.row == 17){
            [self chooseEduDegree];
        }else if (indexPath.row == 18){
            [self chooseOccupyCategory];
        }else if (indexPath.row == 20){
            [self chooseReligon];
        }
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 16) {
        return 88;
    }else{
        return 44;
    }
}

#pragma mark - UIPickerViewDelegate And DataSource
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 3;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return [self.provinces count];
    }else if (component == 1){
        Province *province = [self.provinces objectAtIndex:[pickerView selectedRowInComponent:0]];
        return [province.citys count];
    }else if (component == 2){
        Province *province = [self.provinces objectAtIndex:[pickerView selectedRowInComponent:0]];
        City *city = [province.citys objectAtIndex:[pickerView selectedRowInComponent:1]];
        return [city.countrys count];
    }else{
        return 0;
    }
}

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel *label = [[UILabel alloc]init];
    label.textColor = [UIColor colorWithHexString:@"333333"];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:14];
    if (component == 0) {
        if (self.provinces.count > row) {
            Province *province = [self.provinces objectAtIndex:row];
            label.text = province.name;
        }
    }else if (component == 1){
        Province *province = [self.provinces objectAtIndex:[pickerView selectedRowInComponent:0]];
        if (province.citys.count > row) {
            City *city = [province.citys objectAtIndex:row];
            label.text =  city.name;
        }
    }else if (component == 2){
        Province *province = [self.provinces objectAtIndex:[pickerView selectedRowInComponent:0]];
        NSInteger selectedRowInComponent1 = [pickerView selectedRowInComponent:1];
        if (province.citys.count > selectedRowInComponent1) {
            City *city = [province.citys objectAtIndex:selectedRowInComponent1];
            if (city.countrys.count > row) {
                Country *c = [city.countrys objectAtIndex:row];
                label.text = c.name;
            }
        }
    }
    return label;
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component == 0) {
        [pickerView selectRow:0 inComponent:1 animated:NO];
        [pickerView selectRow:0 inComponent:2 animated:NO];
        [pickerView reloadComponent:1];
        [pickerView reloadComponent:2];
    }else if (component == 1){
        [pickerView selectRow:0 inComponent:2 animated:NO];
        [pickerView reloadComponent:2];
    }
}

-(void)fetchModel:(id)model{
    self.eduDegreeModel = model;
    self.educationDegreeField.text = self.eduDegreeModel.name;
}


#pragma mark - UITextDelegate
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if ([string isEqualToString:@""]) {
        return YES;
    }
    if (textField.text.length == 17) {
        NSString *idNo = [NSString stringWithFormat:@"%@%@",textField.text,string];
        [self beginLoading];
        [[NetWorkerManager sharedInstance]getPersonalInfoWithCardId:idNo successBlock:^(BaseModel *model) {
            [self endLoading];
            NSDictionary *source = model.data;
            BOOL success = [source[@"success"] boolValue];
            if (success) {
                NSDictionary *data = [source objectForKey:@"data"];
                if ([data isKindOfClass:[NSDictionary class]]) {
                    NSString *birthDay = [NSString stringWithFormat:@"%@-%@-%@",[[idNo substringFromIndex:6]substringToIndex:4],[[idNo substringFromIndex:10]substringToIndex:2],[[idNo substringFromIndex:12]substringToIndex:2]];
                    self.birthdayField.text = birthDay;
                    NSString *sex = data[@"sex"];
                    self.sexField.text = sex;
                    [[NetWorkerManager sharedInstance]getPropertyDictWithDomainName:@"性别" successBlock:^(BaseModel *m) {
                        if (model.success) {
                            for (PropertyDictItemModel *item in m.list) {
                                if ([item.displayName containsString:self.sexField.text]) {
                                    self.sexModel = item;
                                }
                            }
                        }
                    } failBlock:^(NSInteger code) {
                        [self endLoading];
                        [self alertSome:kNetWorkFail];
                    }];
                    NSString *provinceCode = data[@"provinceCode"];
                    NSString *province = data[@"province"];
                    Province *p = [[Province alloc]init];
                    p.name = province;
                    p.code = provinceCode;
                    self.hujiProvince = p;
                    NSString *cityCode = data[@"cityCode"];
                    NSString *city = data[@"city"];
                    City *c = [[City alloc]init];
                    c.name = city;
                    c.code = cityCode;
                    self.hujiCity = c;
                    NSString *district = data[@"district"];
                    NSString *districtCode = data[@"districtCode"];
                    Country *co = [[Country alloc]init];
                    co.name = district;
                    co.code = districtCode;
                    self.hujiCountry = co;
                    self.locateAddressField.text = [NSString stringWithFormat:@"%@%@%@",province,city,district];
                }
            }else{
                NSString *errmsg = [source objectForKey:@"errmsg"];
                [self alertSome:errmsg];
            }
        } failBlock:^(NSInteger code) {
            [self endLoading];
            [self alertSome:kNetWorkFail];
        }];
        return YES;
    }
    return YES;
}

@end
